import Qust from "/js/Qust.js";
import { $, $$ } from "/js/JSelect.js";
const qust = Qust.create({
	baseUrl: "http://www.rushmc.top/api/",
});

/* 事件绑定 */
if (localStorage.getItem("uid") == undefined) {
	$$.navigateTo("/html/login.html");
}
$("#select-pics").change((e) => {
	let files = e.target.files;
	if (files.length == 0) return false;
	window.files = files;

	let previewTemplate = ``;
	for (let i = 0; i < files.length; i++) {
		let src = window.URL.createObjectURL(files[i]);
		previewTemplate += `<div class="container"><img src=${src}></div>`;
	}
	$("#pics-preview").setHtml(previewTemplate);
});
$("#submit-new").click(() => {
	let title = $("#input-title").value;
	let content = $("#input-content").value;
	if (title == "" || content == "") {
		$$.showModal({
			title: "填写错误",
			content: `请检查你的输入`,
		});
		return false;
	}

	let data = {
		userId: localStorage.getItem("uid"),
		content,
		title,
		hasPicture: 0,
	};
	if (window.files != undefined) {
		data.hasPicture = 1;
		data.files = window.files;
	}
	qust.request({
		url: "dynamicState/insert",
		data,
	}).then((res) => {
		if (res.data.code === 200) {
			$$.showModal({
				title: "发布成功",
				content: `${res.data.msg}`,
			}).then(() => {
				$$.navigateTo("/html/index.html");
			});
		} else {
			$$.showModal({
				title: "发布失败",
				content: `${res.data.msg}`,
			});
		}
	});
});
